//
//  ConfigReader.m
//  automaker
//
//  Created by Vescky on 14-6-23.
//  Copyright (c) 2014年 Vescky. All rights reserved.
//

#import "ConfigReader.h"
#import "RegexKitLite.h"

@implementation ConfigReader
@synthesize templateProjectName,latestSdkVersion,defaultAndroidProjectPath,defaultIosProjectPath,defaultTemplatePath,defaultAndroidLibsPath,deploymentSdkVersion,appBundleIdHeadString,workSpaceFolderPath,outAndroidProjectPath,outIosProjectPath,outputAppPath,outputLogFilesPath,incomeBundleName,incomeBundlePath,scriptFilesPath,merchantAppsPath,wipServerIP,prodServerIP,wipXmppIP,prodXmppIP,testProdServerIP,testProdXmppIP,testWipServerIP,testWipXmppIP;

static ConfigReader *defaultConfigReader;

+ (id)shareConfigReader {
    if (!defaultConfigReader) {
        defaultConfigReader = [[ConfigReader alloc] init];
    }
    return defaultConfigReader;
}

- (void)setMode:(int)_mode {
    mode = _mode;
    
}

- (void)setRunPlatform:(NSString*)_runPlatform {
    runPlatform = _runPlatform;
    
}

- (NSString*)getConfigFilePath {
    //优先查找该类型的配置，若无则用默认配置
    NSString *configPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"auto-compiler.config"];
    return configPath;
}


- (void)parseForPathWithWorkFolder:(NSString*)wFolder {
    NSString *configPath = [self getConfigFilePath];
    NSString *configContent = [NSString stringWithContentsOfFile:configPath encoding:NSUTF8StringEncoding error:nil];
    configContent = [configContent stringByReplacingOccurrencesOfRegex:@"#.*?\n" withString:@"\n"];//去除注释内容
    NSArray *arr = [configContent componentsSeparatedByString:@"\n"];
    if ([arr count] <= 0) {
        return;
    }
    for (int i = 0; i < [arr count]; i++) {
        NSString *str = [arr objectAtIndex:i];
        if (str.length > 0) {
            NSArray *arr1 = [str componentsSeparatedByString:@"="];
            if ([arr1 count] == 2) {
                NSString *str1 = [arr1 objectAtIndex:0];
                if ([str1 isEqualToString:@"template_project_name"]) {
                    self.templateProjectName = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"latest_sdk_version"]) {
                    self.latestSdkVersion = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"deploy_sdk_version"]) {
                    self.deploymentSdkVersion = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"work_folder"] && !wFolder) {
                    //wordFolder为空，读取配置的
                    self.workSpaceFolderPath = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"app_bundleid_head"]) {
                    self.appBundleIdHeadString = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"app_income_bundle_name"]) {
                    self.incomeBundleName = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"wip_server_ip"]) {
                    self.wipServerIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"prod_server_ip"]) {
                    self.prodServerIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"default_template_folder"]) {
                    self.defaultTemplatePath = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"default_android_libs_folder"]) {
                    self.defaultAndroidLibsPath = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"wip_xmpp_ip"]) {
                    self.wipXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"prod_xmpp_ip"]) {
                    self.prodXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_wip_xmpp_ip"]) {
                    self.testWipXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_prod_xmpp_ip"]) {
                    self.testProdXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_wip_server_ip"]) {
                    self.testWipServerIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_prod_server_ip"]) {
                    self.testProdServerIP = [arr1 objectAtIndex:1];
                }
                
            }
        }
    }
    
}


@end
